# pip3 show pymysql 在命令行中执行命令安装MySQLClient包
# pip install cryptography 允许 PyMySQL 使用 sha256_password 或 caching_sha2_password 认证方法
import pymysql

try:
    # 建立连接和创建游标同上
    # con = pymysql.connect(host='82.156.199.130', port=3306, user='liqian', passwd='liqian', db='math_score')
    # con = pymysql.connect(host='www.liqiantw.icu', port=3306, user='liqian', passwd='liqian', db='math_score')
    con = pymysql.connect(host='localhost', port=3306, user='root', passwd='liqian', db='math_score')
    cur = con.cursor()
    
    print("执行查询")
    cur.execute("select * from score order by date desc limit 0,3")  # 执行查询
    
    # 获取所有结果行
    rows = cur.fetchall()  # 这将返回一个包含所有结果的元组列表
    
    # 遍历每一行并打印出来
    for row in rows:
        print(row)

except Exception as e:
    print(f"发生错误: {e}")  # 打印异常信息

finally:
    # 确保关闭游标和连接，即使发生了异常
    if cur:
        cur.close()
    if con:
        con.close()